using System;
using System.Threading.Tasks;
using Infrastructure;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using NiuX.AspNetCore;
using NiuX.Guids;

namespace NiuX.Common.Tests;

public class TestBase
{
    protected readonly WebApplication WebApp;

    protected IGuidGenerator GuidGenerator { get; }

    //protected ITianJiDownloadApiService TianJiDownloadApiService { get; }


    public TestBase()
    {
        var builder = WebApplication.CreateBuilder();
        builder.Services.AddInfrastructureLayer(builder.Configuration);
        //WebApp = App.WebApplication = builder.Build();

        GuidGenerator = GetService<IGuidGenerator>();
        //TianJiDownloadApiService = GetService<ITianJiDownloadApiService>();
    }

    protected T GetService<T>()
    {
        return WebApp.Services.GetService<T>()!;
    }

    protected Guid NextId()
    {
        return GuidGenerator.Create();
    }

    protected async Task ExecuteInScope(Func<IServiceProvider, Task> func)
    {
        await using var scope = WebApp.Services.CreateAsyncScope();
        await func(scope.ServiceProvider);
    }


}